package com.cvicse.jrcompetition01.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cvicse.jrcompetition01.common.lang.Result;
import com.cvicse.jrcompetition01.entity.MechanismContact;
import com.cvicse.jrcompetition01.entity.dto.MechanismDto;
import com.cvicse.jrcompetition01.mapper.MechanismContactMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 作者:kissshotwang@163.com
 * @since 2022-07-22
 */
@RestController
@RequestMapping("/mechanism-contact")
public class MechanismContactController extends BaseController {
    @Resource
    MechanismContactMapper mechanismContactMapper;


    @PostMapping("/add")
    @Transactional
    public Result add(@RequestBody MechanismContact mechanismContact) {
        mechanismContactMapper.insert(mechanismContact);
        return Result.succ("恭喜你，操作成功");
    }

    @GetMapping("/mechanismContactList")
    public Result departmentList(String mechanismName) {
        IPage iPage = mechanismContactMapper.selectPageVo(getPage(),mechanismName);
        return Result.succ(iPage);
    }

    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestBody Long[] ids) {
        //Arrays.asList() 数组转列表 因为mybatis-plus的removeByIds方法参数是list类型
        List<Long> idList = new ArrayList<Long>(Arrays.asList(ids));
        mechanismContactService.removeByIds(idList);
        return Result.succ("恭喜你，操作成功");
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        MechanismContact mechanismContactById = mechanismContactService.getById(id);
        return Result.succ(mechanismContactById);
    }

    @PostMapping("/update")
    @Transactional
    public Result update(@RequestBody MechanismContact mechanismContact ) {
        mechanismContactMapper.updateById(mechanismContact);
        return Result.succ("恭喜你，操作成功");
    }

}
